'use strict';
const express = require('express');
const router = express.Router();
const retUtil = require('../util/retUtil');
const usersRouterUtil = require('./routerUtil/usersRouterUtil');
const userCenterService = require('../../service/userCenterService');
const moment = require('moment');


router.get('/',function (req, res, next) {
    res.send('respond with a uc - user resource');
});
router.use(function (req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    next();
});


/*
* uc-用户实名认证(绑定身份证)
*
* @author 王俊
* @date 2018-07-18
*
* 参数: device
* 参数: userId
* 参数: realName
* 参数: idCardNum
* 参数[可选]: idCardThumbs
*
* 接口文档地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3889
* */
function bindIdCard(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const idCardNum = retUtil.stringFromReq(req, 'idCardNum');
    const realName = retUtil.stringFromReq(req, 'realName');
    // 可选参数, 暂不处理
    const idCardThumbs = retUtil.arrayFromReq(req, 'idCardThumbs');

    if(!userId || userId <=0) {
        return retUtil.ret_err_json('userId 错误', res);
    }
    if(!idCardNum || idCardNum.length <=0) {
        return retUtil.ret_err_json('idCardNum 错误', res);
    }
    if(!realName || realName.length <=0) {
        return retUtil.ret_err_json('realName 错误', res);
    }
    const opt = {
        device: device,
        userId: userId,
        idCardNum: idCardNum,
        realName: realName,
        idCardThumbs: idCardThumbs
    };
    userCenterService.bindIdCard(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });

}
router.get('/bindIdCard', bindIdCard);
router.post('/bindIdCard', bindIdCard);


/*
* uc-用户申请提现
*
* @author 王俊
* @date 2018-07-18
*
* 参数: device
* 参数: userId
* 参数: cashAmount
* 参数: account
* 参数: accountPlatform
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3883
*
* */
function applyCashReward(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const cashAmount = retUtil.numberFromReq(req, 'cashAmount');
    const account = retUtil.stringFromReq(req, 'account');
    const accountPlatform = retUtil.numberFromReq(req, 'accountPlatform');

    if(!userId || userId <=0) {
        return retUtil.ret_err_json('userId 错误', res);
    }
    if(!cashAmount || cashAmount <=0) {
        return retUtil.ret_err_json('cashAmount 错误', res);
    }
    if(!account || account.length <=0) {
        return retUtil.ret_err_json('account 错误', res);
    }
    if(!accountPlatform || (([10,20].indexOf(accountPlatform)<0) && !(accountPlatform>200 && accountPlatform<300) )) {
        return retUtil.ret_err_json('accountPlatform 错误', res);
    }

    // 注意: 此处迁移,暂时直接依赖用户中心服务, 小程序使用时,  若为微信提现, 需增加前置步骤获取用户的的小程序对应的 openid
    const opt = {
        device: device,
        userId: userId,
        cashAmount: cashAmount,
        account: account,
        accountPlatform: accountPlatform
    };
    userCenterService.applyCashReward(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        const timeStr = new moment().format('YYYY-MM-DD HH:mm:ss');
        const timestamp = new moment().unix();
        const retData = {
            cashAmount : cashAmount,
            account : account,
            accountPlatform : accountPlatform,
            applyTime : timeStr,
            applyTimestamp : timestamp
        };
        return retUtil.ret_succ_json(retData, res);
    });
}
router.get('/applyCashReward', applyCashReward);
router.post('/applyCashReward', applyCashReward);


/*
* uc-获取用户信息
*
* @author 王俊
* @date 2018-07-18
*
* 参数: device
* 参数: userId
* 参数: withOrganization
* 参数: withReward
* 参数: withWallet
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3859
*
* */
function getUserInfo(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const withOrganization = retUtil.numberFromReq(req, 'withOrganization') || 0;
    const withReward = retUtil.numberFromReq(req, 'withReward') || 0;
    const withWallet = retUtil.numberFromReq(req, 'withWallet') || 0;

    if(!userId || userId <=0) {
        return retUtil.ret_err_json('userId 错误', res);
    }

    const opt = {
        device: device,
        userId: userId,
        withOrganization: withOrganization,
        withReward: withReward,
        withWallet: withWallet
    };

    userCenterService.getUserInfo(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getUserInfo', getUserInfo);
router.post('/getUserInfo', getUserInfo);


/*
* uc-获取用户收益
*
* @author 王俊
* @date 2018-07-18
*
* 参数: device
* 参数: userId
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3877
*
* */
function getUserActRewardInfo(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');

    if(!userId || userId <=0) {
        return retUtil.ret_err_json('userId 错误', res);
    }

    const opt = {
        device: device,
        userId: userId
    };
    userCenterService.getUserActRewardInfo(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getUserActRewardInfo', getUserActRewardInfo);
router.post('/getUserActRewardInfo', getUserActRewardInfo);



/*
* uc-获取用户收益明细
*
* @author 毛凯
* @date 2018-07-20
*
* 参数: device
* 参数: userId
* 参数: pageNum
* 参数: pageSize
* 参数: withTotalPage
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3880
*
* */
function getUserActRewardDetail(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const pageNum = retUtil.numberFromReq(req, 'pageNum');
    const pageSize = retUtil.numberFromReq(req, 'pageSize');
    const withTotalPage = retUtil.numberFromReq(req, 'withTotalPage') || 0;


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    if(!userId || userId <=0 ||pageNum<0 || !pageSize || pageSize<=0) {
        return retUtil.ret_err_json('参数有误错误', res);
    }

    const opt = {
        device: device,
        userId: userId,
        pageNum: pageNum,
        pageSize: pageSize,
        withTotalPage: withTotalPage
    };

    userCenterService.getUserActRewardDetail(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getUserActRewardDetail', getUserActRewardDetail);
router.post('/getUserActRewardDetail', getUserActRewardDetail);


/*
* uc-获取用户私有码
*
* @author 毛凯
* @date 2018-07-20
*
* 参数: device
* 参数: userId
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3874
*
* */
function getUserPrivateCouponCode(req, res, next) {

    var device = retUtil.numberFromReq(req, 'device');
    var userId = retUtil.numberFromReq(req, 'userId');


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    if(!userId || userId <=0) {
        return retUtil.ret_err_json('user_id', res);
    }

    var opt = {
        device: device,
        userId: userId
    };

    userCenterService.getUserPrivateCouponCode(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getUserPrivateCouponCode', getUserPrivateCouponCode);
router.post('/getUserPrivateCouponCode', getUserPrivateCouponCode);

/*
* uc-小程序登录并自动注册
*
* @author 王俊
* @date 2018-07-19
*
* 参数: device
* 参数: unionId
* 参数: openId
* 参数: no
* 参数: wxInfo
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3856
*
* */
function wxLoginAndAutoRegister(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const unionId = retUtil.stringFromReq(req, 'unionId');
    const openId = retUtil.stringFromReq(req, 'openId');
    const no = retUtil.numberFromReq(req, 'no');
    const wxInfo = retUtil.objectFromReq(req, 'wxInfo');

    if(!unionId || unionId.length <=2) {
        return retUtil.ret_err_json('unionId 错误', res);
    }
    if(!openId || openId.length <=2) {
        return retUtil.ret_err_json('openId 错误', res);
    }
    if(!no || no < 200 || no > 300) {
        return retUtil.ret_err_json('no 错误', res);
    }
    if(!wxInfo) {
        return retUtil.ret_err_json('wxInfo 错误', res);
    }

    const opt = {
        device : device,
        unionId : unionId,
        openId : openId,
        accountType : no,
        wxInfo : wxInfo
    };

    userCenterService.wxLoginAndAutoRegister(opt, function (error, data) {
        if(error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res);
    });


}
router.get('/wxLoginAndAutoRegister', wxLoginAndAutoRegister);
router.post('/wxLoginAndAutoRegister', wxLoginAndAutoRegister);
//==========================================================================
module.exports = router;